#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtNetwork>
#include<QTextEdit>
#include<QMetaObject>
#include <synchapi.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->textEdit->setReadOnly(true);
    ui->textEdit->setTextInteractionFlags(Qt::NoTextInteraction);
    ui->textEdit->viewport()->setCursor(Qt::ArrowCursor);
    tcpClient = new QTcpSocket(this);
    QString localIP = getLocalIP();
    ui->comboServer->addItem(localIP);
    connect(tcpClient, SIGNAL(connected()), this, SLOT(do_connected()));
    connect(tcpClient, SIGNAL(disconnected()), this, SLOT(do_disconnected()));
    connect(tcpClient, &QTcpSocket::stateChanged, this, &MainWindow::do_socketStateChange);
    connect(tcpClient, SIGNAL(readyRead()), this, SLOT(do_socketReadyRead()));
    ui->spinPort->setValue(666);
}

MainWindow::~MainWindow()
{
    delete ui;

}
void MainWindow::do_connect(QString s1)
{
    QMetaObject::invokeMethod(ui->pushButton_3, "clicked", Qt::QueuedConnection);
    ui->lineEdit->setText(s1);
    QMetaObject::invokeMethod(ui->pushButton, "clicked", Qt::QueuedConnection);
}
QString MainWindow::getLocalIP()
{
    QString hostName = QHostInfo::localHostName();
    QHostInfo hostInfo = QHostInfo::fromName(hostName);
    QString localIP = "";
    QList<QHostAddress> addrList = hostInfo.addresses();
    if (addrList.isEmpty())
        return localIP;
    foreach (QHostAddress aHost, addrList)
        if (QAbstractSocket::IPv4Protocol == aHost.protocol())
        {
            localIP = aHost.toString();
            break;
        }
    return localIP;
}
void MainWindow::do_connected()
{
    ui->textEdit->append("**已连接到服务器");
    ui->textEdit->append("**peer address:" + tcpClient->peerAddress().toString());
    ui->textEdit->append("**peer port:" + QString::number(tcpClient->peerPort()));
}

void MainWindow::do_disconnected()
{
    ui->textEdit->append("**已断开与服务器的连接");
}

void MainWindow::do_socketStateChange(QAbstractSocket::SocketState socketState)
{
    switch (socketState)
    {
    case QAbstractSocket::UnconnectedState:
        ui->label->setText("socket状态：UnconnectedState");
        break;
    case QAbstractSocket::HostLookupState:
        ui->label->setText("socket状态：HostLookupState");
        break;
    case QAbstractSocket::ConnectingState:
        ui->label->setText("socket状态：ConnectingState");
        break;
    case QAbstractSocket::ConnectedState:
        ui->label->setText("socket状态：ConnectedState");
        break;
    case QAbstractSocket::BoundState:
        ui->label->setText("socket状态：BoundState");
        break;
    case QAbstractSocket::ClosingState:
        ui->label->setText("socket状态：ClosingState");
        break;
    case QAbstractSocket::ListeningState:
        ui->label->setText("socket状态：ListeningState");
    }
}

void MainWindow::do_socketReadyRead()
{
    while (tcpClient->canReadLine())
        ui->textEdit->append("[in] " + tcpClient->readLine());
}

void MainWindow::on_pushButton_3_clicked()
{
    QString addr = ui->comboServer->currentText();
    quint16 port = ui->spinPort->value();
    tcpClient->connectToHost(addr, port);
}

void MainWindow::on_pushButton_2_clicked()
{
    if (tcpClient->state() == QAbstractSocket::ConnectedState)
        tcpClient->disconnectFromHost();
}

void MainWindow::on_pushButton_clicked()
{
    QString msg = ui->lineEdit->text();
    ui->textEdit->append("[out] " + msg);
    ui->lineEdit->clear();
    ui->lineEdit->setFocus();
    QByteArray str = msg.toUtf8();
    str.append('\n');
    tcpClient->write(str);
}



void MainWindow::on_pushButton_4_clicked()
{
    ui->textEdit->clear();
}

